package com.futuremap.ws.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;

import com.google.common.base.Predicate;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
	

	//注入配置文件中的项目名称
    @Value("${server.context-path}")
    private String contextPath;
 
    /**
     * 构建 swagger2 api 文档的详细信息函数
     * @return
     */
    private ApiInfo initApiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("websocket Platform API")//大标题
                .version( "1.0")//版本
                .description("websocket系统API")//描述
                .contact(new Contact("developer", "", "developer@futuremap.com"))
                .license("The System Server, Version 1.0")//网站链接显示文字
                //.licenseUrl("https://wwwxz.blog.csdn.net/")//网站链接
                .build();
 
        return apiInfo;
    }
 
    /**
     * swagger2的配置文件，这里可以配置swagger2的一些基本的内容，比如扫描的包等等
     * @return
     */
    @Bean
    public Docket restfulApi() {
        System.out.println("http://localhost:8666" + contextPath + "/swagger-ui.html");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(initApiInfo())
                .groupName("RestfulApi")
                //.genericModelSubstitutes(DeferredResult.class)
                .genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(true)
                .forCodeGeneration(false)
                .pathMapping(contextPath) // base，最终调用接口后会和paths拼接在一起
                .select()
                 //加了ApiOperation注解的类，才生成接口文档
                 //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                 //暴露接口地址的包路径（即此包下的类，才生成接口文档）
                .apis(RequestHandlerSelectors.basePackage("com.futuremap.ws.controller"))
                .paths(PathSelectors.any())//自定义的过滤规则
                //.paths(doFilteringRules())//自定义的过滤规则
                .build();
    }
 

    /**
     * 设置过滤规则
     * 这里的过滤规则支持正则匹配
     * @return
     */
    private Predicate<String> doFilteringRules() {
    	return PathSelectors.regex("/api*");
//        return or(
//                regex("/testUser.*"),
//                regex("/hello.*")
//        );
    }
    


}
